package com.fh.controller.system.timer;

import com.fh.controller.collection.Collection;
import com.fh.controller.system.tools.SmsUtlis;
import com.fh.service.building.bill.BillService;
import com.fh.service.building.contract.ContractService;
import com.fh.service.role.ShsRoleService;
import com.fh.util.DateUtil;
import com.fh.util.PageData;
import org.apache.commons.lang.StringUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by caozengling on 2017/10/31.
 */
@DisallowConcurrentExecution
public class TimerSendMsg implements Job {

    private BillService billService = (BillService) ApplicationContextUtil.getBean("billService");
    private ShsRoleService shsRoleService = (ShsRoleService) ApplicationContextUtil.getBean("shsRoleService");
    private ContractService contractService = (ContractService) ApplicationContextUtil.getBean("contractService");

    @Override
    public void execute(JobExecutionContext context) {
        this.sendMsgs();

    }

    private void sendMsgs() {


        try {

            //发送给工作人员， 给有该权限的人发送信息，客户，给改客户到期的人发送信息
            List<PageData> userList = authListAllUser();
            for (PageData pageData : userList) {
                //权限和 人不能为空
                if(pageData.containsKey("AUTH_CODE") && pageData.containsKey("USER_ID")){
                    if(StringUtils.isNotBlank(pageData.getString("AUTH_CODE")) || StringUtils.isNotBlank(pageData.getString("PHONE")) || StringUtils.isNotBlank(pageData.getString("NAME"))){
                       //  合同查看权限： HTQX_CK ，逾期/收费提醒：CWQX_SF
                        String auth_code = pageData.getString("AUTH_CODE");
                        if(auth_code.indexOf("CWQX_SF")!=-1){
                            PageData pd = new PageData();
                              pd.put("phone", pageData.getString("PHONE"));
                            pd.put("name", pageData.getString("NAME"));
                            //逾期
                            sendMsgOverdueBill(pd);
                            //账单收费提醒
                            sendMsgChargeReminding(pd);
                        }else  if(auth_code.indexOf("HTQX_CK")!=-1){
                            PageData pd = new PageData();
                            pd.put("phone", pageData.getString("PHONE"));
                            pd.put("name", pageData.getString("NAME"));
                            //合同合同过期提醒
                              sendMsgExpiredReminding(pd);
                            //合同将到期提醒
                              sendMsgDiesCedit(pd);
                        }else  if(auth_code.indexOf("CBGL_CB")!=-1){
                            PageData pd = new PageData();
                            pd.put("phone",  pageData.getString("PHONE"));
                            pd.put("name", pageData.getString("NAME"));
                            //抄表日
                             sendMsgMeterReadingDay(pd);

                        }else  if(auth_code.indexOf("SPQX_HTSP")!=-1){
                            PageData pd = new PageData();
                            pd.put("phone",  pageData.getString("PHONE"));
                            pd.put("name", pageData.getString("NAME"));
                            //合同审批 短信通知
                            sendMsgContract(pd);
                        } else  if(auth_code.indexOf("SPQX_ZDSP")!=-1){
                            PageData pd = new PageData();
                            pd.put("phone",  pageData.getString("PHONE"));
                            pd.put("name", pageData.getString("NAME"));
                            //账单审批 短信通知
                            sendMsgContract(pd);
                        }


                    }

                }

            }

            //付款提醒日
            sendMsgToChargeReminding();
            //续租退租提醒日
            sendMsgRentingAndRetreating();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Title: 逾期数据
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgOverdueBill(PageData pageData) throws Exception {

        PageData pd = new PageData();
        //逾期提醒  到期日期后一天 提醒
        List<PageData> list = billService.findOverdueBill(pd);
        for (PageData pdData : list) {
            pageData.put("type", "1");
            pageData.put("context", pdData.getString("COMPANY_NAME"));
            SmsUtlis.sendSms(pageData);
        }

    }


    /**
     * `
     *
     * @Title: 合同过期提醒
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgExpiredReminding(PageData pageData) throws Exception {

        PageData pd = new PageData();
        //合同过期提醒  到期日期后一天 提醒
        List<PageData> list = billService.findExpiredReminding(pd);
        for (PageData pdData : list) {
            pageData.put("type", "2");
            pageData.put("context", pdData.getString("BYNAME"));
            SmsUtlis.sendSms(pageData);
        }

    }

    /**
     * `
     *
     * @Title: 合同将到期提醒
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgDiesCedit(PageData pageData) throws Exception {

        PageData pd = new PageData();
        //合同将到期提醒
        List<PageData> list = billService.findDiesCedit(pd);
        for (PageData pdData : list) {
            pageData.put("type", "3");
            pageData.put("context", pdData.getString("BYNAME"));
            SmsUtlis.sendSms(pageData);
        }

    }




    /**
     * `
     *
     * @Title: 账单收费提醒
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgChargeReminding(PageData pageData) throws Exception {
        PageData pd = new PageData();
        //合同将到期提醒
        List<PageData> list = billService.findChargeReminding(pd);
        for (PageData pdData : list) {
            pageData.put("type", "4");
            pageData.put("context", pdData.getString("COMPANY_NAME"));  //租户
            pageData.put("bill_name", pdData.getString("BILL_NAME"));  //账单类型
            pageData.put("total_price", pdData.getString("TOTAL_PRICE")); //账号金额
            pageData.put("pay_date", pdData.getString("PAY_DATE"));   //应收租日
            pageData.put("signer_tel", pdData.getString("SIGNER_TEL"));  //租户联系电话
            SmsUtlis.sendSms(pageData);
        }

    }
    /**
     * `
     *
     * @Title: 付款提醒日
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgToChargeReminding() throws Exception {
        PageData pageData = new PageData();
        PageData pd = new PageData();
        //合同将到期提醒
        List<PageData> list = billService.findChargeReminding(pd);
        for (PageData pdData : list) {
            pageData.put("type", "5");
            pageData.put("phone", pdData.getString("PHONE"));
            pageData.put("name", pdData.getString("NAME"));
            pageData.put("context", pdData.getString("COMPANY_NAME"));  //租户
            pageData.put("bill_name", pdData.getString("BILL_NAME"));  //账单类型
            pageData.put("total_price", pdData.getString("TOTAL_PRICE")); //账号金额
            pageData.put("pay_date", pdData.getString("PAY_DATE"));   //应收租日
            pageData.put("signer_tel", pdData.getString("SIGNER_TEL"));  //租户联系电话

            SmsUtlis.sendSms(pageData);
        }

    }

    /**
     * `
     *
     * @Title: 续租退租提醒日
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgRentingAndRetreating() throws Exception {
        PageData pageData = new PageData();
        PageData pd = new PageData();
        //合同将到期提醒
        List<PageData> list = billService.findDiesCedit(pd);
        for (PageData pdData : list) {
            pageData.put("type", "6");
            pageData.put("byname", pdData.getString("BYNAME"));  //客户别名
            pageData.put("business_name", pdData.getString("BUSINESS_NAME"));  //商圈
            pageData.put("building_name", pdData.getString("BUILDING_NAME"));  //楼宇
            pageData.put("room_num", pdData.getString("ROOM_NUM"));  //项目
            pageData.put("phone", pdData.getString("SIGNER_TEL"));  //客户手机号
            pageData.put("end_tx_days", pdData.getString("END_TX_DAYS"));  //提前几天提醒
            SmsUtlis.sendSms(pageData);
        }

    }

    /**
     * `
     *
     * @Title: 滞纳金执行日
     * @reason
     * @author :  陈豪
     * @date 2018/7/1  findLateFee
     */
    public void sendMsgLateFee() throws Exception {
        PageData pageData = new PageData();
        PageData pd = new PageData();
        //滞纳金执行日
        List<PageData> list = billService.findLateFee(pd);
        for (PageData pdData : list) {
            pageData.put("type", "7");
            pageData.put("byname", pdData.getString("BYNAME"));  //客户别名
            pageData.put("bill_name", pdData.getString("BILL_NAME"));  //账单类型
            pageData.put("building_name", pdData.getString("BUILDING_NAME"));  //楼宇
            pageData.put("total_price", pdData.getString("TOTAL_PRICE"));  //账单金额
            pageData.put("phone", pdData.getString("SIGNER_TEL"));  //客户手机号
            pageData.put("con_end_date", pdData.getString("CON_END_DATE"));  //应付款日
            SmsUtlis.sendSms(pageData);
        }

    }


    /**
     * `
     *
     * @Title: 抄表日
     * @reason
     * @author :  陈豪
     * @date 2018/7/1
     */
    public void sendMsgMeterReadingDay(PageData pageData) throws Exception {
        PageData pd = new PageData();
        //抄表日
        List<PageData> list = billService.findMeterReadingDay(pd);
        for (PageData pdData : list) {
            pageData.put("type", "8");
            SmsUtlis.sendSms(pageData);
        }

    }

    //合同审批 短信通知
    public void sendMsgContract(PageData pd) throws Exception {
        PageData PD_USER = new PageData();
        PD_USER.put("USER_ID", pd.get("USER_ID"));
        List<PageData> list = contractService.listcontractingShenHeMSG(PD_USER);
        if (list.size() > 0) {//有数据 说明有权限 给这个用户发送一条提醒
            pd.put("type", "8");
            SmsUtlis.sendSms(pd);
        }
    }

    //账单审批 短信通知
    public void sendMsgBill(PageData pd) throws Exception {
        PageData PD_USER = new PageData();
        PD_USER.put("USER_ID", pd.get("USER_ID"));
        List<PageData> list = billService.findBillCheck(PD_USER);
        if (list.size() > 0) {//有数据 说明有权限 给这个用户发送一条提醒
            pd.put("type", "9");
            SmsUtlis.sendSms(pd);
        }
    }

    public List<PageData> authListAllUser() throws Exception {
        PageData pageData = new PageData();
        List<PageData> list = shsRoleService.authListAllUser(pageData);
        return list;
    }


}